package com.fc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fc.context.BaseContext;
import com.fc.dto.AssessmentDTO;
import com.fc.dto.pageQuery.AssessmentPageQueryDTO;
import com.fc.entity.Assessment;
import com.fc.exception.AssessmentException;
import com.fc.mapper.AssessmentMapper;
import com.fc.result.PageResult;
import com.fc.service.IAssessmentService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;


@Service
public class AssessmentServiceImpl extends ServiceImpl<AssessmentMapper, Assessment> implements IAssessmentService {
    @Override
    public void savaAssessment(AssessmentDTO assessmentDTO) {
        // 校验分数是否合法
        justScore(assessmentDTO);
        //校验是否已经存在该司机的记录
        LocalDate now = LocalDate.now();
        int year = now.getYear();
        int month = now.getMonthValue();
        if (justDriver(BaseContext.getCurrentId(), year, month)) {
            throw new AssessmentException("本月份该司机已存在考核记录");
        }
        Assessment assessment = getAssessment(assessmentDTO);
        //记录当前时间
        assessment.setYear(year);
        assessment.setMonth(month);
        assessment.setCreateTime(LocalDateTime.now());
        assessment.setCreateUser(BaseContext.getCurrentId());
        save(assessment);
    }


    @Override
    public void updateAssessment(AssessmentDTO assessmentDTO) {
        // 校验分数是否合法
        justScore(assessmentDTO);
        Assessment assessment = getAssessment(assessmentDTO);
        updateById(assessment);
    }

    @Override
    public PageResult listAssessment(AssessmentPageQueryDTO assessmentPageQueryDTO) {
        Page<Assessment> page = new Page<>(assessmentPageQueryDTO.getPageNum(), assessmentPageQueryDTO.getPageSize());
        Long driverId = assessmentPageQueryDTO.getDriverId();
        Integer score = assessmentPageQueryDTO.getScore();
        Page<Assessment> pageResult = lambdaQuery()
                .eq(driverId != null, Assessment::getDriverId, driverId)
                .ge(score != null, Assessment::getScore, score)
                .orderByDesc(Assessment::getMonth)
                .page(page);
        return new PageResult(pageResult.getTotal(), pageResult.getRecords());
    }

    @Override
    public List<Assessment> detail(Long driverId) {
        QueryWrapper<Assessment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id", driverId).orderByDesc("create_time");
        return list(queryWrapper);
    }

    private void justScore(AssessmentDTO assessmentDTO) {
        Integer score = assessmentDTO.getScore();
        if (score < -50 || score > 50) {
            throw new AssessmentException("分数不合法");
        }
    }


    private boolean justDriver(Long currentId, int year, int month) {
        // 获取司机ID和月份
        QueryWrapper<Assessment> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("driver_id", currentId)
                .eq("year", year)
                .eq("month", month);
        Assessment assessment = getOne(queryWrapper);
        return assessment != null;
    }

    private Assessment getAssessment(AssessmentDTO assessmentDTO) {
        Assessment assessment = BeanUtil.copyProperties(assessmentDTO, Assessment.class);
        //记录当前时间
        assessment.setUpdateTime(LocalDateTime.now());
        assessment.setUpdateUser(BaseContext.getCurrentId());
        return assessment;
    }


}
